package ogl.app;

import ogl.vecmath.Vector;
import ch.aplu.xboxcontroller.XboxControllerListener;
import xbox.XboxController;
import xbox.XboxControllerAdapter;

//Select the factory we want to use.
//It is a bit ugly to do it at several points...though 
import static ogl.vecmathimp.FactoryDefault.vecmath;

public class InputXbox {
	private Input input;
	private XboxController xc;
	
	private ThumbPosition leftThumb = new ThumbPosition(0,0);
	private ThumbPosition rightThumb = new ThumbPosition(0,0);
	
	public static final int KEY_A          = 500;
	public static final int KEY_B          = 501;
	public static final int KEY_X          = 502;
	public static final int KEY_Y          = 503;
	public static final int KEY_LEFTTHUMB  = 504;
	public static final int KEY_RIGHTTHUMB = 505;
	public static final int KEY_LB		   = 506;
	public static final int KEY_LT		   = 507;
	public static final int KEY_RB		   = 508;
	public static final int KEY_RT		   = 509;
	
	public InputXbox(Input input) {
		this.input = input;
		xc = new XboxController();
		xc.addXboxControllerListener(new MyXboxControllerAdapter());
		xc.setLeftThumbDeadZone(0.2);
	    xc.setRightThumbDeadZone(0.2);
	}
	
	private class MyXboxControllerAdapter extends XboxControllerAdapter implements XboxControllerListener {
	    public void buttonA(boolean pressed) {
	      if (pressed) 
	    	  input.addKey(KEY_A);
	      else
	    	  input.removeKey(KEY_A);
	    }

	    public void buttonB(boolean pressed) {
	    	if (pressed) 
		    	  input.addKey(KEY_B);
		      else
		    	  input.removeKey(KEY_B);
	    }

	    public void buttonX(boolean pressed) {
	    	if (pressed) 
		    	  input.addKey(KEY_X);
		      else
		    	  input.removeKey(KEY_X);
	    }

	    public void buttonY(boolean pressed) {
	    	if (pressed) 
		    	  input.addKey(KEY_Y);
		      else
		    	  input.removeKey(KEY_Y);
	    }

	    public void back(boolean pressed) {
	     
	    }

	    public void start(boolean pressed) {
	      
	    }

	    public void leftShoulder(boolean pressed) {
	    	if (pressed) 
		    	  input.addKey(KEY_LT);
		      else
		    	  input.removeKey(KEY_LT);
	    }

	    public void rightShoulder(boolean pressed) {
	    	if (pressed) 
		    	  input.addKey(KEY_RT);
		      else
		    	  input.removeKey(KEY_RT);
	    }

	    public void leftThumb(boolean pressed) {
	    	if (pressed) 
		    	  input.addKey(KEY_LEFTTHUMB);
		      else
		    	  input.removeKey(KEY_LEFTTHUMB);
	    }

	    public void rightThumb(boolean pressed) {
	    	if (pressed) 
		    	  input.addKey(KEY_RIGHTTHUMB);
		      else
		    	  input.removeKey(KEY_RIGHTTHUMB);
	    }

	    public void dpad(int direction, boolean pressed) {

	    }

	    public void leftTrigger(double value) {
	    	
	    }

	    public void rightTrigger(double value) {
	    	
	    }

	    public void leftThumbMagnitude(double magnitude) {
	    	leftThumb.setPosition((float) magnitude);
	    }

	    public void leftThumbDirection(double direction) {
	      	leftThumb.setAngle((float) direction);
	    }

	    public void rightThumbMagnitude(double magnitude) {
	    	rightThumb.setPosition((float) magnitude);
	    }

	    public void rightThumbDirection(double direction) {
	    	rightThumb.setAngle((float) direction);
	    }
	  }
	
	protected ThumbPosition getLeftThumbPosition() {
		return leftThumb;
	}
	
	protected ThumbPosition getRightThumbPosition() {
		return rightThumb;
	}
	
	public void release() {
		xc.release();
	}
	
	public class ThumbPosition {
		private float position;
		private float angle;
		
		public ThumbPosition(float position, float angle) {
			this.position = position;
			this.angle = angle;
		}
		
		public float getPosition() {
			return position;
		}
		
		public void setPosition(float p) {
			position = p;
		}
		
		public float getAngle() {
			return angle;
		}
		
		public void setAngle(float a) {
			angle = a;
		}
	}

}
